﻿#if WINDOWS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO.IsolatedStorage;
using System.IO;
using DARE.Properties;

namespace DARE
{
    public class CStorage : IStorage
    {
        #region Fields

        private PlayerIndex m_index;
        private string m_dir;
        private DriveInfo m_info;

        #endregion

        #region Property

        public bool IsSynchronize { get; private set; }

        public long TotalSpace
        {
            get { return m_info.TotalSize; }
        }

        public long FreeSpace
        {
            get { return m_info.TotalFreeSpace; }
        }

        #endregion

        #region Event

        public event Action<IStorage> StorageReady;
        public event Action<IStorage> StorageFailed;

        #endregion

        #region CTOR

        public CStorage(PlayerIndex _index = PlayerIndex.One)
        {
            m_index = _index;
            IsSynchronize = false;
            Synchronize();
        }

        #endregion

        #region Method

        public void CreateDirectory(string directory)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            if (!Directory.Exists(Path.Combine(m_dir, directory)))
                Directory.CreateDirectory(Path.Combine(m_dir, directory));
        }

        public System.IO.Stream CreateFile(string file)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return File.Create(Path.Combine(m_dir, file));
        }

        public void DeleteDirectory(string directory)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            string[] files = Directory.GetFiles(Path.Combine(m_dir, directory), "*", SearchOption.AllDirectories);
            foreach (var file in files)
                File.Delete(file);
            Directory.Delete(Path.Combine(m_dir, directory), true);
        }

        public void DeleteFile(string file)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            if (File.Exists(Path.Combine(m_dir, file)))
                File.Delete(Path.Combine(m_dir, file));
        }

        public bool DirectoryExists(string directory)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return Directory.Exists(Path.Combine(m_dir, directory));
        }

        public void Dispose()
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            IsSynchronize = false;
        }

        public bool FileExists(string file)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return File.Exists(Path.Combine(m_dir, file));
        }

        public string[] GetDirectoryNames()
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return Directory.GetDirectories(m_dir);
        }

        public string[] GetDirectoryNames(string searchPattern)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return Directory.GetDirectories(m_dir, searchPattern);
        }

        public string[] GetFileNames()
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return Directory.GetFiles(m_dir);
        }

        public string[] GetFileNames(string searchPattern)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return Directory.GetFiles(m_dir, searchPattern);
        }

        public System.IO.Stream OpenFile(string file, System.IO.FileMode fileMode)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return File.Open(Path.Combine(m_dir, file), fileMode);
        }

        public System.IO.Stream OpenFile(string file, System.IO.FileMode fileMode, System.IO.FileAccess fileAccess)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return File.Open(Path.Combine(m_dir, file), fileMode, fileAccess);
        }

        public System.IO.Stream OpenFile(string file, System.IO.FileMode fileMode, System.IO.FileAccess fileAccess, System.IO.FileShare fileShare)
        {
            if (!IsSynchronize)
                throw new Exception(Resources.StorageNotSynchronized);
            return File.Open(Path.Combine(m_dir, file), fileMode, fileAccess, fileShare);
        }

        public void Synchronize()
        {
            m_dir = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("Saved", Enum.GetName(typeof(PlayerIndex), m_index)));
            if (!Directory.Exists(m_dir))
                Directory.CreateDirectory(m_dir);
            if (m_info != null)
            {
                string root = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory());
                m_info = (from el in DriveInfo.GetDrives() where el.Name == root select el).SingleOrDefault();
            }
            if (StorageReady != null)
                StorageReady.Invoke(this);
            IsSynchronize = true;
        }

        #endregion
    }
}

#endif
